<?php

namespace Jammiest\SwigeCore\Servers;

use Jammiest\SwigeCore\Tools\Config;
use Symfony\Component\Console\Input\InputInterface;
use Jammiest\SwigeCore\Handlers\EventHandler;

class Tcp extends Server
{
    public static function getInstance(array $config): \Swoole\Server
    {
        if (!self::$server) {
            /**
             * @var EventHandler
             */
            $eventHandler = new class extends EventHandler {};
            $server = new \Swoole\Server($config['host'], $config['port']);
            $server->set($config['settings']);

            $server->on('receive', function ($server, $fd, $reactorId, $data) use ($eventHandler) {
                $eventHandler->onReceive($server, $fd, $reactorId, $data);
            });
            self::$server = $server;
        }

        return self::$server;
    }
}
